package com.sc.bean;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;

public class WordInfo implements WritableComparable<WordInfo> {

	private Text word;
	private IntWritable count;
	
	public WordInfo() {
		super();
	}

	public WordInfo(Text word, IntWritable count) {
		super();
		this.word = word;
		this.count = count;
	}

	public Text getWord() {
		return word;
	}

	public void setWord(Text word) {
		this.word = word;
	}

	public IntWritable getCount() {
		return count;
	}

	public void setCount(IntWritable count) {
		this.count = count;
	}

	@Override
	public void write(DataOutput out) throws IOException {
		// TODO Auto-generated method stub
		word.write(out);
		count.write(out);
	}

	@Override
	public void readFields(DataInput in) throws IOException {
		// TODO Auto-generated method stub
		word = new Text();
		word.readFields(in);
		count = new IntWritable();
		count.readFields(in);
	}

	@Override
	public String toString() {
		return word + "\t" + count;
	}

	/*
	 * 用于实现复杂对象的排序
	 */
	@Override
	public int compareTo(WordInfo o) {
		// TODO Auto-generated method stub
		if (this.count.get() > o.getCount().get()) {
			return 1;//大的排在后面
		}
		else if (this.count.get() == o.getCount().get()) {
			//很关键！按词频比较排序后，如果相等，就要求再按“词”排序，如果直接写返回0，会将词频相等的词都去重！
			return this.word.compareTo(o.getWord());
		}else {
			return -1;
		}
	}

}
